<?php
	
	require_once(ROOTDIR.'source/service/class/class.main.php');
	
	class _Account_number extends Main
	{
	
		private static $account_number = 'account_number';
		
		public static function addAccount_numberInfo($data)
		{
			$set = join(',',self::setMySQLParam($data));
			if(!empty($set))
			{
				$sql = sprintf("insert into %s set %s ",self::$account_number,$set);
				fileLog($sql);
				return self::getMysqlInstance()->insert_sql($sql);
			}
			else
			{
				return false;
			}
		}
		
		public static function delAccount_numberInfo($where)
		{
			$sql = sprintf("delete from %s where id= '%s'",self::$account_number,$where['id']);
			fileLog($sql);
			return self::getMysqlInstance()->diy_update_sql($sql);
		}
		
		public static function setAccount_numberInfo($where,$data)
		{
			$set = join(',',self::setMySQLParam($data));
			if(!empty($set) && !empty($where['id']))
			{
				$sql = sprintf("update %s set %s where id='%s'",self::$account_number,$set,$where['id']);
				fileLog($sql);
				return self::getMysqlInstance()->diy_update_sql($sql);
			}
			else
			{
				return false;
			}
		}
		
		public static function getAccount_numberInfo($wheres)
		{
			$where = join(' and ',self::setMySQLParam($wheres));
			$sql = sprintf("select * from %s where %s",self::$account_number,$where);
			fileLog($sql);
			$res = self::getMysqlInstance()->query($sql);
			
			return $res[0];
		}
		
		public static function getAccount_numberList($wheres,$page=array(),$between=array(),$sort=array())
		{
			$where = join(' and ',self::setMySQLParam($wheres));
			
			if(empty($where))
			{
				$where = ' 1=1 ';
			}
			
			if(isset($between['start']) && isset($between['end']))
			{
				$where .= ' and `addtime` between "'.$between['start'].'" and "'.$between['end'].'" ';
			}
			
			if(!empty($page))
			{
				$sqlCount = "select count(id) as count from account_number where ".$where;
			
				fileLog($sqlCount,'debug');
			
				$resCount = self::getMysqlInstance()->query($sqlCount);
				
				if(!empty($resCount[0]))
				{
					$pagenum=ceil($resCount[0]['count']/$page['rows']); 
				}
				else
				{
					$pagenum=ceil(0/$page['rows']); 
				}
				
				$offset=($page['page']-1)*$page['rows'];   
				$limit = " limit $offset,".$page['rows'];
			}
			$select = 'select * ';
			if(is_array($sort) && $sort)
			{
				$order =' order by ';
				foreach($sort as $k => $v)
				{
					$ordertemp[] =" `{$k}` ".self::getMysqlInstance()->strescape($v);
				}
				
				$order .= implode(',', $ordertemp);
			}
			$sql = sprintf("%s from %s where %s %s %s",$select,self::$account_number,$where,$order,$limit);
			fileLog($sql);
			if(!empty($page))
			{
				$result['list'] = self::getMysqlInstance()->query($sql);
				
				$result['pages'] = $pagenum;
			}
			else
			{
				$result =  self::getMysqlInstance()->query($sql);
			}
			return $result;
		}
		
	}